package com.cc.servlet;

import com.alibaba.fastjson2.JSON;
import com.cc.entity.Employee;
import com.cc.entity.Page;
import com.cc.service.EmployeeService;
import com.cc.service.impl.EmployeeServiceImpl;
import com.cc.util.DateUtil;
import com.cc.util.FileUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
@WebServlet("/employee")
//文件上传 maxFileSize:最大传输的大小
@MultipartConfig(maxFileSize = 1024 * 1024 * 2)
public class EmployeeServlet extends HttpServlet {
    EmployeeService employeeService=new EmployeeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //允许所有来源访问
        resp.addHeader("Access-Control-Allow-Origin","*");
        //允许访问的方式
        resp.addHeader("Access-Control-Allow-Method","POST,GET");
        String method = req.getParameter("method")==null?
                "list":req.getParameter("method");
        switch (method) {
            case "list":
                List(req, resp);
                break;
            case "edit":
                edit(req,resp);
                break;
            case "det":
                det(req,resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "update":
                upd(req,resp);
                break;
            default:
                break;
        }
    }

    private void det(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id=Integer.parseInt(req.getParameter("id"));
        boolean b = employeeService.det(id);
        if(b) {
            resp.getWriter().write("success");
        } else {
            resp.getWriter().write("fail");
        }
    }

    public void upd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Employee employee = new Employee();
        employee.setEmpName(req.getParameter("empName"));
        employee.setDepId(Integer.valueOf(req.getParameter("departmentId")));
        employee.setPosition(req.getParameter("position"));
        employee.setStartDate(DateUtil.parseDate(req.getParameter("hireDate")));
        employee.setPhone(req.getParameter("phone"));
        employee.setImgUrl(req.getParameter("photo"));
        int id=Integer.parseInt(req.getParameter("id"));
        boolean b = employeeService.upd(employee,id);
        if(b) {
            resp.getWriter().write("success");
        } else {
            resp.getWriter().write("fail");
        }
    }

    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        Employee employee = employeeService.editById(id);
        String json = JSON.toJSONString(employee, "yyyy-MM-dd");
        resp.getWriter().write(json);
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Employee employee = new Employee();
        employee.setEmpName(req.getParameter("empName"));
        employee.setDepId(Integer.valueOf(req.getParameter("departmentId")));
        employee.setPosition(req.getParameter("position"));
        employee.setStartDate(DateUtil.parseDate(req.getParameter("hireDate")));
        employee.setPhone(req.getParameter("phone"));
        employee.setImgUrl(req.getParameter("photo"));
        boolean b = employeeService.add(employee);
        if(b){
            resp.getWriter().write("success");
        }else {
            req.setAttribute("msg","注册失败，请联系管理员");
        }
    }


    public void List(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNum = req.getParameter("pageNum");
        String empName=req.getParameter("empName");
        Page<Employee> page=new Page<>();
        if (pageNum!=null&&!pageNum.equals("0")){
            page.setPageNum(Integer.parseInt(pageNum));
        }
        page=employeeService.EmployeeList(page,empName);
        String json= JSON.toJSONString(page,"yyyy-MM-dd");
        resp.getWriter().write(json);
    }
}
